﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace Sherwood.Web
{
    public class QueryParameterCollection : ICollection<QueryParameter>
    {
        private readonly Dictionary<string, QueryParameter> _dict = new Dictionary<string, QueryParameter>();

        public QueryParameterCollection() : this(string.Empty)
        {
        }

        public QueryParameterCollection(string query)
        {
            foreach (var raw in (query ?? string.Empty).Trim('&', '?').Split('&'))
            {
                if (raw.Contains("="))
                    Add(raw);
            }
        }

        public int Count
        {
            get { return _dict.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public IEnumerator<QueryParameter> GetEnumerator()
        {
            return ((IEnumerable<QueryParameter>)_dict.Values).GetEnumerator();
        }

        public override string ToString()
        {
            string url = string.Empty;
            foreach (var queryParameter in _dict.Values)
                url += "&" + queryParameter.RawKeyAndValue;
            return "?" + url.Trim('&');
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Add(string rawKeyAndValue)
        {
            Add(new QueryParameter(rawKeyAndValue));
        }

        public void Add(string key, DateTime value)
        {
            Add(new QueryParameter(key, string.Empty) { ValueAsDate = value });
        }

        public void Add(string key, int value)
        {
            Add(new QueryParameter(key, string.Empty) { ValueAsInt = value });
        }

        public void Add(string key, Guid value)
        {
            Add(new QueryParameter(key, string.Empty) { ValueAsGuid = value });
        }

        public void Add(string key, string value)
        {
            Add(new QueryParameter(key, value));
        }

        public void Add(QueryParameter parameter)
        {
            if (parameter == null) throw new ArgumentNullException("parameter");
            var key = parameter.DictionaryKey;
            if (_dict.ContainsKey(key))
            {
                int i = 1;
                while (_dict.ContainsKey(key + i))
                    i++;
                key += i;
                parameter.DictionaryKey = key;
            }
            _dict.Add(key, parameter);
        }

        public void Clear()
        {
            _dict.Clear();
        }

        public bool Contains(QueryParameter item)
        {
            return _dict.ContainsValue(item);
        }

        public void CopyTo(QueryParameter[] array, int arrayIndex)
        {
            _dict.Values.CopyTo(array, arrayIndex);
        }

        public bool Remove(string key)
        {
            if (key == null) throw new ArgumentNullException("key");
            return _dict.Remove(key.ToLowerInvariant());
        }

        public bool Remove(QueryParameter profile)
        {
            if (profile == null) throw new ArgumentNullException("profile");
            return _dict.Remove(profile.DictionaryKey);
        }

        public bool TryGetValue(string key, out QueryParameter queryParameter)
        {
            if (key == null) throw new ArgumentNullException("key");
            return _dict.TryGetValue(key.ToLower(), out queryParameter);
        }

        public QueryParameter this[string key]
        {
            get
            {
                QueryParameter queryParameter;
                TryGetValue(key, out queryParameter);
                return queryParameter;
            }
        }
    }
}